package com.example.anni.zhuzhoujia;

/**
 * 第三方组件:
 * viewPager
 */

import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;

import com.example.anni.zhuzhoujia.bogege.NewsFragment;
import com.example.anni.zhuzhoujia.bogege.SpecialFragment;
import com.example.anni.zhuzhoujia.tianya12345.BrokeFragment;
import com.example.anni.zhuzhoujia.tianya12345.CityFragment;
import com.example.anni.zhuzhoujia.xiaowan.UserFragment;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    private FragmentTabHost tabHost;
    private String[] titles = {
            "新闻", "专题", "同城", "爆料", "我"
    };
    /**
     * 首页下方的图标
     */
    private int[] icons = {
            R.drawable.icon_news_sel,
            R.drawable.icon_special_sel,
            R.drawable.icon_city_sel,
            R.drawable.icon_broke_sel,
            R.drawable.icon_me_sel
    };
    /**
     * 碎片类
     */
    private Class[] fragments = {
            NewsFragment.class,
            SpecialFragment.class,
            CityFragment.class,
            BrokeFragment.class,
            UserFragment.class
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //隐藏ActionBar
        //getSupportActionBar().hide();

        //沉浸式状态栏
        //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        creatFragment();

    }

    /**
     * 为TanHost添加对应的碎片
     */
    private void creatFragment() {
        //获取FragmentTabhost
        tabHost = (FragmentTabHost) findViewById(R.id.tabhost);
        //将FragmentTabhost和实际的内容区关联
        tabHost.setup(this, getSupportFragmentManager(), R.id.real_content);
        //创建tabSpec
        for (int i = 0; i < icons.length; i++) {
            TabHost.TabSpec tabSpec = tabHost.newTabSpec(titles[i]);
            tabSpec.setIndicator(createItem(i));
            Bundle bundle = new Bundle();

            tabHost.addTab(tabSpec, fragments[i], bundle);
        }
        //为选项卡标签设置背景
        TabWidget tabWidget = tabHost.getTabWidget();

        //去掉TabSpec之间的分割线
        tabWidget.setDividerDrawable(null);
    }

    /**
     * 创建选项卡标签的视图
     * @param index 传过来的i值
     * @return
     */
    private View createItem(int index) {
        View root = getLayoutInflater().inflate(R.layout.tabspec_item, null);
        ImageView ivIcon = (ImageView) root.findViewById(R.id.tabspec_icon);
        ivIcon.setImageResource(icons[index]);

        TextView tvTitle = (TextView) root.findViewById(R.id.tabspec_title);
        tvTitle.setText(titles[index]);

        return root;
    }

    /**
     * 物理返回键响应
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitBy2Click(); //调用双击退出函数
        }
        return false;
    }

    /**
     * 双击退出函数
     */
    private static Boolean isExit = false;

    private void exitBy2Click() {
        Timer tExit = null;
        if (isExit == false) {
            isExit = true; // 准备退出
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false; // 取消退出
                }
            }, 2000); // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务

        } else {
            finish();
            System.exit(0);
        }
    }

}